package tree;

import tree.data.TreeNode;


/**
 *
 * 二叉树的直径
 *
 * https://leetcode.cn/problems/diameter-of-binary-tree/?envType=study-plan-v2&envId=top-100-liked
 */
public class DiameterOfBinaryTree {

    int max=0;

    public int diameterOfBinaryTree(TreeNode root) {
        maxDepth(root);
        return max;
    }

    public int maxDepth(TreeNode root) {
        if (root==null){
            return 0;
        }
        int l=maxDepth(root.left);
        int r=maxDepth(root.right);

        max= Math.max(l + r,max);

        return Math.max(l,r)+1;
    }
}
